package cn.lxm.samples.auth_server.config.support;

import com.google.gson.Gson;
import org.apache.commons.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

/**
 * @author LXM
 * @Title: oauth2
 * @Description:
 * @date 2018/8/16下午7:12
 */
@Component
@PropertySource("classpath:sso_config.properties")
public class OAuth2Service {

    @Value("${spring.oauth.token.url}")
    String tokenUrl;

    @Autowired
    private Gson gson;


    public String getToken(String username, String password) {

        //请求URL
        String URL = tokenUrl + "?grant_type=password&username=" + username + "&password=" + password + "";
        //通用的Client Deatil
//        String auth= Base64.encodeBase64String()
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders requestHeaders = new HttpHeaders();
        requestHeaders.add("Authorization", "Basic VUkyOjEyMzQ1");
        HttpEntity<String> requestEntity = new HttpEntity<String>(null, requestHeaders);
        ResponseEntity responseEntity = restTemplate.postForEntity(URL, requestEntity, String.class);
        return responseEntity.getBody().toString();

    }

}
